#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10, M = 2e6 + 10;
int n;
int arr[N];
int tree[M];
int l[N], r[N];
int lowbit(int x)
{
	return x & -x;
}
void add(int x, int v)
{
	for (int i = x; i <= M; i+=lowbit(i)) {
		tree[i] += v;
	}
}
int get(int x)
{
	int ans = 0;
	for (int i = x; i; i -= lowbit(i)) {
		ans += tree[i];
	}
	return ans;
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
	}
	for (int i = 1; i <= n; i++) {
		int y = arr[i];
		l[i] = get(n) - get(y);
		add(y, 1);
	}
	/*for (int i = 1; i <= n; i++) {
		cout << l[i] << " ";
	}
	cout << endl;*/
	memset(tree, 0, sizeof(tree));
	for (int i = n; i >= 1; i--) {
		int y = arr[i];
		r[i] = get(n) - get(y);
		add(y, 1);
	}
	/*for (int i = 1; i <= n; i++) {
		cout << r[i] << " ";
	}*/
	LL ans = 0;
	for (int i = 1; i <= n; i++) {
		ans += min(l[i], r[i]);
	}
	cout << ans << endl;
	return 0;
}